package com.ks.member.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.dto.OperateBase;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.exception.GunsException;
import com.ks.core.util.ToolUtil;
import com.ks.member.entity.MemberLevel;
import com.ks.member.dao.MemberLevelMapper;
import com.ks.member.service.IMemberLevelService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会员-会员等级表 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-17
 */
@Service
public class MemberLevelServiceImpl extends ServiceImpl<MemberLevelMapper, MemberLevel> implements IMemberLevelService {
    @Resource
    private MemberLevelMapper memberLevelMapper;

    @Override
    public int addMemberLevel(OperationContext context, MemberLevel memberLevel) {
        memberLevel.setTenantID(context.getTenantId().intValue());
        memberLevel.setOrgID(context.getOrgId().intValue());
        memberLevel.setOrgSubID(context.getOrgSubId().intValue());
        memberLevel.setCreateUserID(context.getUserId().intValue());
        Integer add = memberLevelMapper.insert(memberLevel);
        return add;
    }

    @Override
    public int deleteMemberLevel(OperationContext context, List<Integer> id) {
        int count = 0;
        if (ToolUtil.isNotEmpty(id)) {
            for (int i = 0; i < id.size(); i++) {
                MemberLevel memberLevel = memberLevelMapper.selectById(id.get(i));
                if (ToolUtil.isNotEmpty(memberLevel)) {
                    memberLevel.setDelMark(Const.DEL_FLAG_DEL);
                    Integer integer = memberLevelMapper.updateById(memberLevel);
                    count += integer;
                }
            }
        }
        return count;
    }


    @Override
    public int updateMemberLevel(OperationContext context, MemberLevel memberLevel) {
        MemberLevel level = memberLevelMapper.selectById(memberLevel.getId());
        if (ToolUtil.isEmpty(level)) {
            throw new GunsException("该会员不存在");
        }
        Integer update = memberLevelMapper.updateById(memberLevel);
        return update;
    }

    @Override
    public Map<String, Object> selectMemberLever(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<MemberLevel> wrapper = new EntityWrapper<>();
        MemberLevel memberLevel = new MemberLevel();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("id".equals(field.getName())) {
                    memberLevel.setId(Integer.valueOf(field.getValue()));
                }
            }
        }
        memberLevel.setDelMark(Const.DEL_FLAG_NORMAL);
        memberLevel.setTenantID(context.getTenantId().intValue());
        if (searchCriteriaBase.startTime != null && searchCriteriaBase.endTime != null) {
            String filterSql = "createTime >'" + searchCriteriaBase.getStartTime()
                    + "'and createTime<'" + searchCriteriaBase.getEndTime() + "'";
            wrapper.addFilter(filterSql);
        }
        wrapper.setEntity(memberLevel);
        RowBounds list = new Page<>(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<MemberLevel> memberLevels = memberLevelMapper.selectPage(list, wrapper);
        Map<String, Object> resultMap = this.isResultMap(memberLevels, searchCriteriaBase);
        return resultMap;
    }

    @Override
    public Map<String, Object> selectAdminMemberLever(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<MemberLevel> wrapper = new EntityWrapper<>();
        MemberLevel memberLevel = new MemberLevel();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("id".equals(field.getName())) {
                    memberLevel.setId(Integer.valueOf(field.getValue()));
                }
                if("tenantID".equals(field.getName())){
                    memberLevel.setTenantID(Integer.valueOf(field.getValue()));
                }
            }
        }
        memberLevel.setDelMark(Const.DEL_FLAG_NORMAL);
        if (searchCriteriaBase.startTime != null && searchCriteriaBase.endTime != null) {
            String filterSql = "createTime >'" + searchCriteriaBase.getStartTime()
                    + "'and createTime<'" + searchCriteriaBase.getEndTime() + "'";
            wrapper.addFilter(filterSql);
        }
        wrapper.setEntity(memberLevel);
        RowBounds list = new Page<>(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<MemberLevel> memberLevels = memberLevelMapper.selectPage(list, wrapper);
        Map<String, Object> resultMap = this.isResultMap(memberLevels, searchCriteriaBase);
        return resultMap;
    }

    public Map<String, Object> isResultMap(List list, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", list.size());
        map.put("limit", searchCriteriaBase.getLimit());
        map.put("pageIndex", searchCriteriaBase.getPageIndex());
        map.put("hasMore", false);
        map.put("items", list);
        return map;

    }


}


